<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/font-awesome.min.css" />
    <link rel="stylesheet" type="text/css" id="yejiancss" />
    <style type="text/css">
    </style>
</head>
<body>
  <header class="aui-bar aui-bar-nav" id="header">
    <a class="aui-pull-left aui-btn" onclick="back()">
        <i class="aui-iconfont aui-icon-left"></i>
    </a>
    <div class="aui-title">阅读记录</div>
    <a class="aui-pull-right aui-btn" tapmode onclick="opensearch()">
        <span class="aui-iconfont aui-icon-search"></span>
    </a>
  </header>
    <div id="main"></div>
    <script type="text/x-dot-template" id="main-temp">
      {{? it.historybook && it.historybook.data && $api.isArray(it.historybook.data) && it.historybook.data.length > 0}}
        <ul class="aui-list aui-media-list aui-list-noborder">
            {{ for(var m=0,mlen=it.historybook.data.length;m<mlen;m++) { }}
              <li class="aui-list-item aui-margin-b-5">
                <div class="aui-media-list-item-inner" tapmode onclick="openhistoryread({{=it.historybook.data[m].book_id}},{{=it.historybook.data[m].colums}},{{=it.historybook.data[m].colum_id}},'{{=it.historybook.data[m].zhangjie}}','{{=it.historybook.data[m].subject}}','{{=it.historybook.data[m].book_name}}','{{=it.historybook.data[m].image}}','{{=it.historybook.data[m].progress}}')">
                  <div class="aui-list-item-media">
                      <img src="{{? it.historybook.data[m].image}}{{=indexSetting.url}}../data/attachment/forum/{{=it.historybook.data[m].image}}{{??}}../image/book.png{{?}}">
                  </div>
                  <div class="aui-list-item-inner">
                      <div class="aui-list-item-text">
                          <div class="aui-list-item-title aui-ellipsis-1">
                              [读至]{{=it.historybook.data[m].subject}}
                          </div>
                      </div>
                      <div class="aui-list-item-text">
                          [图书]{{=it.historybook.data[m].book_name}}
                      </div>
                  </div>
                </div>
              </li>
            {{ } }}
        </ul>
      {{??}}
        <h1 class="aui-text-danger aui-text-center aui-margin-t-15">暂无阅读记录</h1>
      {{?}}
  </script>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/dot.js"></script>
<script type="text/javascript" src="../script/aui-toast.js"></script>
<script type="text/javascript">
var toast = new auiToast({});
toast.loading({
    title:"loading...",
    duration:2000
})
var indexSetting = $api.getStorage('appinfo');
if ($api.getStorage('localstore')) {
    indexSetting.localstore = ($api.getStorage('localstore'));
} else {
    indexSetting.localstore = {
        data: []
    };
}
apiready = function() {
    autocheckyejian();
    ajaxuser();
    $api.fixStatusBar($api.dom('header'));
    api.parseTapmode();
    if (api.statusBarAppearance) {
        if (api.systemType == 'android') {
            indexSetting.statbar = 25;
        } else {
            indexSetting.statbar = 20;
        }
        api.setStatusBarStyle({
            style: 'dark'
        });
    } else {
        indexSetting.statbar = 0;
    }

    indexSetting.userinfo = $api.getStorage('userinfo');
    indexSetting.fs = api.require('fs');
    initload();
};

//  初始化
function initload() {
    indexSetting.history = $api.getStorage('history');
    if (indexSetting.userinfo && indexSetting.userinfo.bbsuid) {
        api.ajax({
            url: indexSetting.url + 'api.php?appkey=' + indexSetting.appkey + '&type=get&module=store&param=bbsuid----' + indexSetting.userinfo.bbsuid,
            method: 'get'
        }, function(ret, err) {
            if (ret) {
                if (ret.status) {
                    var temp = $api.text($api.byId("main-temp"));
                    var dottemp = doT.template(temp);
                    ret.historybook = indexSetting.history;
                    var html = dottemp(ret);
                    $api.html($api.byId('main'), html);
                    toast.hide();
                    $api.setStorage('localstore', ret.bookstore);
                } else {
                    toast.fail({
                        title: ret.error,
                        duration: 2000
                    });
                }
            } else {
                var msg = JSON.stringify(err);
                toast.fail({
                    title: msg.msg,
                    duration: 2000
                });
            }
        });
    } else {
        var ret = {};
        var temp = $api.text($api.byId("main-temp"));
        var dottemp = doT.template(temp);
        ret.historybook = indexSetting.history;
        ret.bookstore = indexSetting.localstore;
        var html = dottemp(ret);
        $api.html($api.byId('main'), html);
        toast.hide()
    }
}

function openhistoryread(book_id, colums, colum_id, zhangjie, subject, book_name, image, progress) {
    if (parseInt(colums) < 1) {
        openbook(book_id);
    } else {
        openzhangjie(book_id, colum_id, zhangjie, subject, book_name, image, progress);
    }
}

//下载
function down(book_id, colum_id) {
    api.download({
        url: indexSetting.url + 'api.php?appkey=' + indexSetting.appkey + '&type=get&module=downbook&param=book_id----' + book_id + ',colum_id----' + colum_id,
        savePath: 'fs://' + book_id + '_' + colum_id + '.txt',
        report: true,
        cache: true,
        allowResume: true
    }, function(ret, err) {
        if (ret.state == 1) {
            indexSetting.download = true;
            return true;
        } else if (ret.state == 2) {
            toast.fail({
                msg: '下载失败',
                duration: 2000
            });
            indexSetting.download = false;
            return false;
        }
    });
}
//阅读
function openzhangjie(book_id, colum_id, zhangjie, subject, book_name, image, progress) {
    api.openWin({
        name: 'read',
        url: './read.html',
        rect: {
            x: 0,
            y: 0,
            w: api.winWidth,
            h: api.Height
        },
        useWKWebView:true,
        historyGestureEnabled:true,
        pageParam: {
            book_id: book_id,
            book_name: book_name,
            image: image,
            desco: '',
            colum_id: colum_id,
            zhangjie: zhangjie,
            subject: subject,
            progress: progress
        },
        bgColor: "#ffffff",
        bounces: false,
        vScrollBarEnabled: true,
        hScrollBarEnabled: false,
        animation: {
            type: "push",
            subType: "from_right",
            duration: 300
        },
        reload: true
    });
}
</script>
</html>